﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EasyQuartz.Domain.Services.Scheduling;
using EasyQuartz.Domain.Jobs;
using EasyQuartz.Web.ViewModels;
using EasyQuartz.Web.Models;

namespace EasyQuartz.Web.Controllers
{
	public class JobController : Controller
	{
		//ISchedulingManager scheduler = SchedulerInstance.Value;
		ISchedulingManager scheduler = new SchedulingManager();

		// GET: /Job/
		public ActionResult Index()
		{
			var viewModel = new JobListViewModel { Jobs = scheduler.Jobs };
			return View(viewModel);
		}

		// AJAX: /Job/RunNow/
		[HttpPost]
		public ActionResult RunNow(string jobName, string jobGroup)
		{
			scheduler.RunJob(jobName, jobGroup);
			var viewModel = new JobListViewModel { Jobs = scheduler.Jobs };
			var result = viewModel.Jobs.Where(item => item.Name.Equals(jobName) && item.Group.Equals(jobGroup)).First();

			UpdateModel(viewModel);
			return Json(result);
		}

		public ActionResult Details(int jobId)
		{
			return View();
		}
	}
}
